home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 163_01 / ftell2.c < prev    next >
Text File  |  1988-01-30  |  2KB  |  34 lines

  1. /*
  2. ** determine current stream position
  3. **
  4. ** ptrname == 0 -- return byte offset mod 512
  5. ** ptrname != 0 -- return sector offset
  6. */
  7. #define FILE int
  8. #define _BUFSIZE 512
  9. #include <streamio.h>
  10.  
  11. extern int errno, _seek();
  12.  
  13. ftell2(stream, ptrname) FILE *stream; int ptrname; {
  14.   int errsave, lowoff, lowpart, highoff;
  15.   errsave = errno;  /* in case someone else wants it preserved */
  16.   errno = 0;  /* assure no prior error */
  17.   lowoff = _seek(stream[_IOB_FD], 0, 1);  /* current byte position */
  18.   highoff = _seek(stream[_IOB_FD], 0, 4);  /* current sector position */
  19.   if(errno) return -1;  /* return if error */
  20.   errno = errsave;  /* restore prior error (if any) */
  21.   lowpart = lowoff % 512;  /* part not also reflected in highoff */
  22.   if(stream[_IOB_FLAG] & _DIRTY) {  /* if buffer in write mode */
  23.     lowoff += (_BUFSIZE - stream[_IOB_CNT]);  /* add number of bytes buffered */
  24.     lowpart += (_BUFSIZE - stream[_IOB_CNT]);  /* again */
  25.     highoff += lowpart / 512;  /* handle any "carry" */
  26.     }
  27.   else {  /* either read mode or unbuffered */
  28.     lowoff -= stream[_IOB_CNT];  /* subtract off number of bytes buffered */
  29.     lowpart -= stream[_IOB_CNT];  /* again */
  30.     highoff += (lowpart-511) / 512;  /* handle any "borrow" */
  31.     }
  32.   return ptrname ? highoff : lowoff;  /* return requested value */
  33.   }
  34.